package com.zhuhjay.boot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhuhjay.constant.MessageConstant;
import com.zhuhjay.domain.entity.OrderSetting;
import com.zhuhjay.domain.http.Result;
import com.zhuhjay.service.OrdersettingService;
import com.zhuhjay.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * ԤԼ 前端控制器
 * </p>
 *
 * @author zhuhjay
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;

    @PostMapping("/upload")
    public Result upload(@RequestPart("excelFile") MultipartFile file){
        try{
            ordersettingService.add(POIUtils.readExcel(file));
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @GetMapping("/ordersettingByMonth")
    public Result getOrdersettingByMonth(String date){
        try{
            List<Map<String, Object>> list = ordersettingService.getOrdersettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            ordersettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

}
